#!/bin/bash                                                        

function enable_ip_forward()
{
  echo 1 > /proc/sys/net/ipv4/ip_forward                              
  sysctl -w net.ipv4.ip_forward=1 1>/dev/null 2>&1

  return
}

function init_iptables_settings()
{
  iptables -F
  iptables -X
  iptables -Z
  iptables -F -t nat
  iptables -X -t nat
  iptables -Z -t nat
  iptables -P INPUT   ACCEPT
  iptables -P OUTPUT  ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -t nat -P PREROUTING   ACCEPT
  iptables -t nat -P POSTROUTING  ACCEPT
  iptables -t nat -P OUTPUT       ACCEPT

  return
}

function load_modules_needed()
{
  modprobe ip_tables 2> /dev/null
  modprobe ip_nat_ftp 2> /dev/null
  modprobe ip_nat_irc 2> /dev/null
  modprobe ip_conntrack 2> /dev/null
  modprobe ip_conntrack_ftp 2> /dev/null
  modprobe ip_conntrack_irc 2> /dev/null

  return
}

function get_remote_addr()
{
  ifconfig "$1" 2>/dev/null | grep "P-t-P" | awk -F":" '{print $3}' | awk '{print $1}'
  return
}

function get_active_ppp()
{
  ppp_itf=""
  for i in {0..9}
  do
    ck_itf=`get_remote_addr "ppp$i" 2>/dev/null`
    if [ "$ck_itf" != "" ]
    then
      ppp_itf="ppp$i"
      echo "$ppp_itf"
      return
    fi
  done
  return
}


my_eth="eth0"
my_net="192.168.1.0/24"
hp_eth="eth0"
hp_net="16.190.0.0/16"
oslo_eth="`get_active_ppp`"
oslo_net="10.0.0.0/8"

enable_ip_forward
init_iptables_settings
load_modules_needed

iptables -t nat -A PREROUTING -i "$my_eth" -p tcp --dport 80 -j REDIRECT --to-port 3128
iptables -t nat -A PREROUTING -i "$my_eth" -p tcp --dport 443 -j REDIRECT --to-port 3128
iptables -o "$my_eth" -t nat -A POSTROUTING -j MASQUERADE


